package moe.bogos.douro.api.common.entity;

import moe.bogos.douro.api.common.Aged;
import moe.bogos.douro.api.common.soul.skill.ChangeableSkillContainer;
import moe.bogos.douro.api.common.soul.skill.Skill;
//import moe.bogos.douro.api.common.soul.SoulBeastInfo;
import net.minecraft.entity.CreatureEntity;
import net.minecraft.entity.EntityType;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.DamageSource;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.world.World;

import java.util.Random;
import java.util.Set;

public abstract class SoulBeastEntity extends CreatureEntity implements ChangeableSkillContainer, Aged
{
    private static int soulBeastMaxAge;
    //private SoulBeastInfo info;
    private SoulRingDropEntity entitySoulRing;
    private double ageIntervalProbability[] = new double[]{0.37, 0.33, 0.18745785, 0.09745785, 0.0047, 0.00235, 0.001179, 0.00625, 0.0003125, 0.0001562, 0.0000781, 0.0000390, 0.0000195};
    private int[] minAge = new int[]{10, 100, 1000, 10000, 100000, 200000, 300000, 400000, 500000, 600000, 700000, 800000, 900000};
    private int[] maxAge = new int[]{99, 999, 9999, 99999, 199999, 299999, 399999, 499999, 599999, 699999, 799999, 899999, 999999};
    
    
    public SoulBeastEntity(EntityType<SoulBeastEntity> type, World worldIn)
    {
        super(type,worldIn);
        //info = new SoulBeastInfo(this.getAge(), "", new Skill[0]);
    }
    
    public static int setSoulBeastAgeLimit(int maxAge)
    {
        soulBeastMaxAge = maxAge;
        return 0;
    }
    
    public static int getSoulBeastAgeLimit()
    {
        return soulBeastMaxAge;
    }
    
    public int setSoulBeastSpawnAge()
    {
        Random random = new Random();
        double sum = 0;
        int ageInterval = random.nextInt(10000000);
        int age = 0;
        int i;
        
        while (age > this.getSoulBeastAgeLimit())//如果age超出最大限制就重新算一遍
        {
            sum = 0;
            for (i = 0; i < 13; i++)
            {
                sum += ageIntervalProbability[i];
                if (ageInterval < (int) (sum * 10000000))
                {
                    age = random.nextInt(maxAge[i] - minAge[i] + 1) + minAge[i];
                    break;
                }
            }
        }
        
        return age;
    }

    @Override
    public void die(DamageSource cause)
    {
        //int commonAge = this.setSoulBeastSpawnAge();
        SoulRingDropEntity soulRing = new SoulRingDropEntity(DAEntities.SOUL_RING, this.getCommandSenderWorld(), this.getAge(), /*info.getName()*/null, cause, new Skill[0]);
        Vector3d pos = position();
        soulRing.absMoveTo(pos.x, pos.y, pos.z, this.yRot, this.xRot);
        this.getCommandSenderWorld().addFreshEntity(soulRing);
        //System.out.println(commonAge);
    }
    
    @Override
    public void readAdditionalSaveData(CompoundNBT compound)
    {
        super.readAdditionalSaveData(compound);
        //info = new SoulBeastInfo(compound.getCompound(Constants.SOUL_DATA));
    }

    @Override
    public void addAdditionalSaveData(CompoundNBT compound)
    {
        super.addAdditionalSaveData(compound);
        //compound.put(Constants.SOUL_DATA, info.writeToNbt());
    }
    
    public Set<Skill> getSkills()
    {
        //return info.getSkills(); TODO
        return null;
    }
    
    public void setSkills(Set<Skill> skills)
    {
        //info.setSkills(skills);
    }
    
    public void setAge(int age)
    {
        //info.setAge(age);
    }
    
    public int getAge()
    {
        //return info.getAge();
        return 0;
    }

    public void setName(String name)
    {
        //info.setName(name);
    }

    @Override
    public ITextComponent getName()
    {
        //return new StringTextComponent(info.getName());
        return null;
    }
}
